package cn.wolfcode.service.impl;

import cn.wolfcode.domain.ExamQuestion;
import cn.wolfcode.domain.Question;
import cn.wolfcode.domain.QuestionItem;
import cn.wolfcode.mapper.ExamQuestionMapper;
import cn.wolfcode.mapper.QuestionItemMapper;
import cn.wolfcode.mapper.QuestionMapper;
import cn.wolfcode.qo.NewExam;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IQuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionItemMapper questionItemMapper;

    @Autowired
    private ExamQuestionMapper examQuestionMapper;


    @Override
    public void save(Question question) {
        questionMapper.insert(question);
    }

    @Override
    public void delete(Long id) {
        questionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Question question) {
        questionMapper.updateByPrimaryKey(question);
    }

    @Override
    public Question get(Long id) {
        return questionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Question> listAll() {
        return questionMapper.selectAll();
    }

    @Override
    public PageInfo<Question> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Question> questions = questionMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Question>(questions);
    }

    @Override
    public List<QuestionItem> selectItemsByQuestionId(Long id) {
        return questionItemMapper.selectItemsByQuestionId(id);
    }

    @Override
    public List<String> selectOptionsByQuestionId(Long id) {
        return questionItemMapper.selectOptionsByQuestionId(id);
    }



    @Override
    public List<HashMap> selectSingleQuestions(Long examId, Long type) {
        List<HashMap> mapList = new ArrayList<>();

        List<Question> questions = questionMapper.selectByType(examId, type);
        for (Question question : questions) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("题目标题",question.getTitle());
            map.put("题目选项",question.getOptions());
            map.put("题目答案",question.getAnswer().get(0));
            map.put("题目分数",question.getScore());
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public List<HashMap> selectMultipleQuestions(Long examId, Long type) {
        List<HashMap> mapList = new ArrayList<>();

        List<Question> questions = questionMapper.selectByType(examId, type);
        for (Question question : questions) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("题目标题",question.getTitle());
            map.put("题目选项",question.getOptions());
            //map.put("题目答案",question.getAnswer().get(0));
            String answer = "";
            List<String> answers = question.getAnswer();
            for (String s : answers) {
                answer = answer + s;
            }
            map.put("题目答案",answer);
            map.put("题目分数",question.getScore());
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public List<HashMap> selectJudgeQuestions(Long examId, Long type) {
        List<HashMap> mapList = new ArrayList<>();

        List<Question> questions = questionMapper.selectByType(examId, type);
        for (Question question : questions) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("题目标题",question.getTitle());
            map.put("题目选项",question.getOptions());
            map.put("题目答案",question.getQJudgeRight()?"对":"错");
            map.put("题目分数",question.getScore());
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public void addQuestions(NewExam newExam){
        //随机生成单选题
        List<Question> singleQuestions = questionMapper.selectRandomQuestions(1L, newExam.getSingleNum());
        List<Question> multipleQuestions = questionMapper.selectRandomQuestions(2L, newExam.getMultipleNum());
        List<Question> judgeQuestions = questionMapper.selectRandomQuestions(3L, newExam.getJudgeNum());
        //将数据加到试卷_题目中间表
        ExamQuestion examQuestion = new ExamQuestion();
        for (Question question : singleQuestions) {
            examQuestion.setExam(newExam.getExam());
            examQuestion.setQuestion(question);
            examQuestion.setScore(newExam.getSingleScore());
            examQuestionMapper.insert(examQuestion);
        }
        for (Question question : multipleQuestions) {
            examQuestion.setExam(newExam.getExam());
            examQuestion.setQuestion(question);
            examQuestion.setScore(newExam.getMultipleScore());
            examQuestionMapper.insert(examQuestion);
        }
        for (Question question : judgeQuestions) {
            examQuestion.setExam(newExam.getExam());
            examQuestion.setQuestion(question);
            examQuestion.setScore(newExam.getJudgeScore());
            examQuestionMapper.insert(examQuestion);
        }
    }


}
